<?php

require_once 'UIControlFactory.php';
require_once PHP_UI_PATH . DS . 'UIControl.php' ;

abstract class BaseUIControlFactory implements UIControlFactory {

    private $filters = array();
    private $usedTypes = array();

    function __construct(array $filters = array()) {
        $this->filters = $filters;
    }

    /**
     *
     * @return UIControl
     */
    public final function create() {
        $instance = $this->makeInstance();
        $this->registerInstance($instance);

        foreach ($this->filters as $filter) {
            if ($filter instanceof UIControlFilter) {
                $instance = $filter->filterControl($instance);
            }
        }

        return $instance;
    }

    protected abstract function makeInstance();

    public function getFilters() {
        return $this->filters;
    }

    public function setFilters(array $filters) {
        $this->filters = $filters;
    }

    public final function getUsedTypes() {
        return $this->usedTypes;
    }

    private function registerInstance($instance) {
        if ($instance instanceof UIControl) {
            $type = get_class($instance);
            if (!isset($this->usedTypes[$type])) {
                $this->usedTypes[$type] = $instance;
            }
        }
    }

}

?>
